package com.naihe.anderson.sms.activity;

import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.constants.NhSmsConstants;
import com.naihe.anderson.sms.util.FileUtil;
import com.naihe.anderson.sms.util.ScreenShot;

public class SreenShotActivity extends Activity{
	
	NhSmsApp nhSmsApp;

	public static final int MSG_WHAT_SHOW_DOTHING = 0;
	public static final int MSG_WHAT_SHOW_SCREENSHOT_NAME = 1;
	
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_WHAT_SHOW_DOTHING:
				screenShot();
				break;
			case MSG_WHAT_SHOW_SCREENSHOT_NAME:
				Toast.makeText(SreenShotActivity.this, "截屏存于" + (String)msg.obj, Toast.LENGTH_SHORT).show();
				toFinish();
				break;
			default:
				break;
			}
		}
		
	};
	
	private void screenShot(){
		if(nhSmsApp.getActivity() != null){
			final String savePath = FileUtil.getPicNotePath();
			if(TextUtils.isEmpty(savePath)){
				Toast.makeText(SreenShotActivity.this, "截屏失败,无SD卡", Toast.LENGTH_SHORT).show();
				toFinish();
			} else {
				new Thread(){

					@Override
					public void run() {
						String filePathName = savePath + NhSmsConstants.SCREENSHOT_PREFIX + new Date().getTime() + ".png";
						ScreenShot.shoot(nhSmsApp.getActivity(), filePathName);
						Message message = new Message();
						message.what = MSG_WHAT_SHOW_SCREENSHOT_NAME;
						message.obj = filePathName;
						mHandler.sendMessage(message);
					}
				}.start();
			}
		} else {
			Toast.makeText(SreenShotActivity.this, "截屏失败", Toast.LENGTH_SHORT).show();
			toFinish();
		}
	}

	ArrayAdapter<String> adapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sreenshot_layout);
		nhSmsApp = (NhSmsApp) getApplication();
		nhSmsApp.setSreenShot(true);
		
		TextView titleText = (TextView) findViewById(R.id.title_text);
		ListView listView = (ListView) findViewById(R.id.item_list);
		
		String[] itemList = new String[]{"截屏"};
		adapter = new ArrayAdapter<String>(SreenShotActivity.this, R.layout.simple_list_height_item, itemList);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if(adapter.getItem(position).equals("截屏")){
					mHandler.sendEmptyMessage(MSG_WHAT_SHOW_DOTHING);
				}
			}
		});
        
		titleText.setText("操作");
	}
	
	private void toFinish(){
		nhSmsApp.setSreenShot(false);
		SreenShotActivity.this.finish();
		System.gc();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 截获按键事件
		if (KeyEvent.KEYCODE_BACK == keyCode) {
			toFinish();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

}
